/**
 * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd.
 * kiran-cc-daemon is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 * Author:     tangjie02 <tangjie02@kylinos.com.cn>
 */

#pragma once

#include <QSharedPointer>
#include "power-backlight-interface.h"

class QGSettings;

namespace Kiran
{
/* 显示器亮度控制。显示器亮度设置适用于笔记本电脑和带背光控制器的显示器，
   台式机的显示器不一定带有背光控制器，因此可能无法通过该模块的接口调节亮度，
   台式机的显示器一般可以直接通过显示器周边的按钮调节亮度 */

class PowerBacklightMonitorsController : public PowerBacklightPercentage
{
    Q_OBJECT

public:
    PowerBacklightMonitorsController(QObject* parent = nullptr);
    virtual ~PowerBacklightMonitorsController(){};

    virtual void init();

    virtual PowerDeviceType getType() override { return PowerDeviceType::POWER_DEVICE_TYPE_MONITOR; };

    // 设置亮度百分比，如果不支持设置或者不存在可设置的显示器则返回false
    virtual bool setBrightness(int32_t percentage) override;
    /* 获取亮度百分比
       如果存在多个显示器有不同的亮度值，只会取其中的一个进行返回
       如果返回负数则表示获取失败
    */
    virtual int32_t getBrightness() override;

    // 增加亮度
    virtual bool brightnessUp() override;
    // 降低亮度
    virtual bool brightnessDown() override;

private:
    void loadBacklightMonitors();
    // 设置单个显示器的亮度百分比
    bool setBrightnessPercentage(QSharedPointer<PowerBacklightAbsolute> absoluteMonitor, int32_t percentage);
    // 获取单个显示器的亮度
    int32_t getBrightnessPercentage(QSharedPointer<PowerBacklightAbsolute> absoluteMonitor);
    // 增加单个显示器亮度
    bool brightnessValueUp(QSharedPointer<PowerBacklightAbsolute> absoluteMonitor);
    // 降低单个显示器亮度
    bool brightnessValueDown(QSharedPointer<PowerBacklightAbsolute> absoluteMonitor);

    int32_t brightnessDiscrete2Percent(int32_t discrete, int32_t levels);
    int32_t brightnessPercent2Discrete(int32_t percentage, int32_t levels);
    int32_t getBrightnessStep(uint32_t levels);

private Q_SLOTS:
    // 更新缓存的亮度百分比并发送信号
    void updateCachedBrightness();

private:
    QSharedPointer<PowerBacklightMonitors> m_backlightMonitors;
    int32_t m_brightnessPercentage;
    QGSettings* m_settings;
};
}  // namespace Kiran